Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(android): allow developers to provide logic for onRenderProcessGone in WebViewListener #6946

Merged
merged 4 commits into from
Oct 6, 2023

Conversation

carlpoole
Copy link
Member

Portals developer app was crashing because they don't have a way to override this method in the WebViewClient in Capacitor. This adds the method to the interface so that it can be overridden via Portals. Not a breaking change.

@carlpoole carlpoole requested review from ItsChaceD and a team September 28, 2023 22:27
@carlpoole carlpoole self-assigned this Sep 28, 2023
@carlpoole carlpoole removed the request for review from ItsChaceD September 28, 2023 22:28
@jcesarmobile
Copy link
Member

The method should return true only if the app is "handling the situation", but there is no guarantee that the app is handling the situation just by having listeners as the listeners are an array of any WebView listeners, not just the onRenderProcessGone listeners, so it could have a listener for other events and not for onRenderProcessGone.
In fact there is a bug in all the webViewListeners checks since they are never null but an empty ArrayList.
I think that could be fixed by changing the return of onRenderProcessGone from the WebViewListener to boolean and check if any of the listeners returned true.

What's the plan for handling the onRenderProcessGone on portals side? could we handle it in Capacitor so portals doesn't have to?

There is an alternate plugin PR where the handling is forwarded to plugins and they can return true if they are handling onRenderProcessGone instead of using listeners. That's similar to how we handle shouldOverrideLoad

Boolean shouldOverrideLoad = plugin.shouldOverrideLoad(url);
if (shouldOverrideLoad != null) {
return shouldOverrideLoad;

#6416

Also, as an alternative if this is going to be a portals only thing, portals could use their own WebViewClient that extends BridgeWebViewClient and implements onRenderProcessGone and that would require no changes in Capacitor.

@carlpoole
Copy link
Member Author

@jcesarmobile I like the solution in #6416 but it only provides access to that one specific WebViewClient method on the Plugin class, when the plugin could in theory just call addWebViewListener on the Bridge as well

public void addWebViewListener(WebViewListener webViewListener) {
and use anything in the interface as well as onRenderProcessGone if it is added there. That WebViewListener can be used in plugins too.

Im tweaking the solution in this PR to give more control over the true/false return state

@carlpoole
Copy link
Member Author

@jcesarmobile also in regards to this

What's the plan for handling the onRenderProcessGone on portals side? could we handle it in Capacitor so portals doesn't have to?

I was thinking about this. In Portals we need to give people full control since we don't know how the WebView is being used in an app, but in Capacitor right now I assume this is just an app crash. This is product decision but it might be nice if an error dialog displays and reloads the activity automatically or something.

if (webViewListeners != null) {
for (WebViewListener listener : bridge.getWebViewListeners()) {
if (listener.onRenderProcessGone(view, detail)) {
return true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should keep iterating to forward the event to all the listeners even if one of them returned true

Copy link

@peitschie peitschie Oct 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noting this feedback hasn't been addressed here yet.
The logical combination will short-circuit, so once a true value is returned, I don't believe any other listener gets informed.

E.g., https://stackoverflow.com/a/8759917

@jcesarmobile
Copy link
Member

@jcesarmobile I like the solution in #6416 but it only provides access to that one specific WebViewClient method on the Plugin class, when the plugin could in theory just call addWebViewListener on the Bridge as well

public void addWebViewListener(WebViewListener webViewListener) {

and use anything in the interface as well as onRenderProcessGone if it is added there. That WebViewListener can be used in plugins too.
Im tweaking the solution in this PR to give more control over the true/false return state

I'm just showing an alternative, which is similar to how the shouldOverrideUrlLoading event was handled, which is previous to the WebViewListener implementation, and shouldOverrideUrlLoading is not exposed on the WebViewListener.

At the moment all the WebViewListener expose methods that return void, while the shouldOverrideUrlLoading is the one returning a boolean and it's implemented in a different way. And onRenderProcessGone returns boolean, so might make sense to do it like shouldOverrideUrlLoading.
But it's also doable to make the WebViewListener return a boolean as demonstrated in your PR.

I have no preference on one over the other, probably makes sense to do all of them the same way.

Copy link
Member

@jcesarmobile jcesarmobile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

The only thing is that this should go to main branch first and then cherry picked into 5.x once it's merged (or if it gets merged)

@carlpoole carlpoole changed the base branch from 5.x to main October 6, 2023 14:41
@carlpoole carlpoole changed the base branch from main to 5.x October 6, 2023 14:42
@carlpoole carlpoole merged commit 34b724a into 5.x Oct 6, 2023
6 checks passed
@carlpoole carlpoole deleted the android-webviewclient-gone branch October 6, 2023 14:43
carlpoole added a commit that referenced this pull request Oct 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants